//
//  RegistViewController.m
//  AppleMarket
//
//  Created by yangy on 24/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "RegistViewController.h"

// view
#import "RegisMainView.h"
// viewModel
#import "RegistViewModel.h"
// controller
#import "InputCodeViewController.h"
#import "WYWebController.h"
// model
#import "ImageCodeMoel.h"

@interface RegistViewController ()
@property (nonatomic, strong) RegisMainView *mainView;
@property (nonatomic, strong) RegistViewModel *viewModel;
@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mainView];
    [self setNagivationBar];
    [self bindViewModel];
}

- (void) setNagivationBar
{
    @weakify(self);
    UIButton *leftButton = [UIButton cm_ButtonWithTitle:@"取消" font:kFont(13) titleColor:kColorWithHex(0xADB7C1)];
    [[leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    UIButton *rightButton = [UIButton cm_ButtonWithTitle:@"去登录" font:kFont(13) titleColor:kColorWithHex(0x3291EB)];
    
    [[rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.navigationController popToRootViewControllerAnimated:YES];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (void) bindViewModel
{
    @weakify(self);
    
    [self.viewModel.sendCodeCkickSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
       // 点击了发送短信验证码
        InputCodeViewController *codeVC = [InputCodeViewController inputCodeViewControllerWithCodeType:CodeTypeSignup];
        codeVC.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85];
        codeVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        codeVC.completeHandler = ^(ImageCodeMoel *codeModel, NSString *inputCodeStr) {
            // 调用发送验证码接口发送验证码
            [self.viewModel.refershSMSCodeCommand execute:RACTuplePack(codeModel.ImgKey,inputCodeStr)];
        };
        [self presentViewController:codeVC animated:YES completion:nil];
    }];
    
    [self.viewModel.registClickSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        InputCodeViewController *codeVC = [InputCodeViewController inputCodeViewControllerWithCodeType:CodeTypeSignup];
        codeVC.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85];
        codeVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        codeVC.completeHandler = ^(ImageCodeMoel *codeModel, NSString *inputCodeStr) {
            // 调用注册接口组册账号
            [self.viewModel.reigisterCommond execute:RACTuplePack(codeModel.ImgKey,inputCodeStr)];
        };
        [self presentViewController:codeVC animated:YES completion:nil];
    }];
    
    [self.viewModel.userAgreeClickSubject subscribeNext:^(id  _Nullable x) {
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"UserAgree" ofType:@"html" inDirectory:@"html"];
        NSString *htmlStr = [[NSString alloc] initWithData: [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:filePath]] encoding:NSUTF8StringEncoding];
        WYWebController *webVC = [WYWebController webControllerWithTitle:@"用户协议" content:htmlStr];
        [self bh_PushViewController:webVC hiddenTabbar:YES];
    }];
}

- (RegisMainView *)mainView
{
    if(!_mainView){
        _mainView = [[RegisMainView alloc] initWithFrame:self.view.bounds viewModel:self.viewModel];
    }
    return _mainView;
}

- (RegistViewModel *)viewModel
{
    if(!_viewModel){
        _viewModel = [[RegistViewModel alloc] init];
    }
    return _viewModel;
}

@end
